using System;
using Slaks.DocumentSerialization.Dictionary;
using Slaks.DocumentSerialization.Document;

namespace Slaks.Graphs.GraphBuilder.SchenkerDictionary
{
	/// <summary>
	/// Summary description for SimpleRepresentationSlaks.
	/// </summary>
	public class SimpleDictionaryRepresentationSchenker : SchenkerDictionaryBuilder, IDictionaryRepresentation
	{
		#region IDictionaryRepresentation Members

		public Graph Build(Document document, DictionaryMap dictionaryMap)
		{
			string documentName = document.FileName;
			int index = documentName.LastIndexOfAny(new char[] {'\\', '/'});
			if (index >= 0 && (index + 1 < documentName.Length)) documentName = documentName.Substring(index + 1);

			Graph graph = new Graph(documentName);

			Vertex vertex;

			//iterate through nodes
			foreach (Node node in document.Nodes)
			{
				//check if the vertex is in the list of frequent nodes from dictionary
				if (dictionaryMap.Contains(node.Id) == false) continue;
				Section textSection = node.GetSectionByType(Section.SectionType.Text);
				if (textSection == null) continue;
				vertex = graph.CreateAddVertex(node.Id);


				//xml provides separate section for meta-description, but shenker algorithm treats meta-description as title
				vertex.Frequency = textSection.TermFrequency;

				//iterate through sentences
				foreach (Sentence sentence in textSection.Sentences)
				{
					AddRelations(document, graph, dictionaryMap, vertex, 1, 1, sentence, Section.SectionType.Text, BuilderType.SimpleDictionaryRepresentationSchenker);
				}
			}


			if (graph.GetVertexCount() <= 0)
				throw new GraphException(String.Format("Length of the graph {0} is zero", documentName));

			return graph;
		}

		public Graph Build(Document document, int nParameter, DictionaryMap dictionaryMap)
		{
			throw new NotImplementedException("SimpleDictionaryRepresentationSlaks does not implement this method");
		}

		#endregion
        protected override string GetBuilderName()
        {
            return "Schenker's Simple By Dictionary Representation";
        }
        protected override string GetBuilderDescription()
        {
            return "Create a graph without labeling the edges, and connect only two neighbouring nodes. It uses only the most frequent nodes in the documents collection";
        }
	}
}